package com.yoomiito.app.ui.fragment.my;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yoomiito.app.R;
import com.yoomiito.app.adapter.cicle.CicleAdapter;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.model.cicle.CicleInfo;
import com.yoomiito.app.present.cicle.MyCirclePresenter;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.RouterUtils;
import com.yoomiito.app.widget.state.ContentType;
import com.yoomiito.app.widget.state.EmptyType;
import com.yoomiito.app.widget.state.StatusManager;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MyCircleActivity extends BaseActivity<MyCirclePresenter> {

    @BindView(R.id.tv_center)
    TextView mTitleTv;
    //    @BindView(R.id.act_my_circle_rcy)
    RecyclerView mRecyclerView;
    //    @BindView(R.id.act_my_circle_refreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    @BindView(R.id.act_my_circle_fmLayout)
    FrameLayout mFrameLayout;
    public static final String MyCircleAct = "MyCircleAct";
    private CicleAdapter adapter;
    private int currentPage = 1;

    @Override
    public int getLayoutId() {
        return R.layout.act_my_circle;
    }

    @Override
    public MyCirclePresenter newP() {
        return new MyCirclePresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mStatusManager = new StatusManager.Builder(this)
                .addType(new ContentType(R.layout.fm_rcy))
                .addType(new EmptyType().setEmptyContent("未发布内容"))
                .build();
        mRecyclerView = mStatusManager.view().findViewById(R.id.fm_rcy);
        mSmartRefreshLayout = mStatusManager.view().findViewById(R.id.fm_refreshLayout);
        mFrameLayout.addView(mStatusManager.view());

        mTitleTv.setVisibility(View.VISIBLE);
        mTitleTv.setText("我的发布");

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        adapter = new CicleAdapter(null, MyCircleAct);
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            CicleInfo.DataBean dataBean = adapter.getData().get(position);
            int type = 1 ;
            switch (dataBean.getOrder_type()) {
                case 1:
                    type = AllCode.TaoBao;
                    break;
                case 2:
                    type = AllCode.Gift;
                    break;
                case 3:
                    type = AllCode.BaoPin;
                    break;
                case 4:
                    type = AllCode.YouPin;
                    break;
            }
            RouterUtils.toGoodsDetail((BaseActivity) context, String.valueOf(dataBean.getProduct_id()), type);
//            if(1==dataBean.getProduct_type()){
            /*  原始代码
            if (!"2".equals(dataBean.getStatus())){
                RouterUtils.toGoodsDetail((BaseActivity) context, String.valueOf(dataBean.getProduct_id()), type);
            }
            */
            //修改后
//            if (1 == dataBean.getOrder_type()) {
//                RouterUtils.toGoodsDetail((BaseActivity) context, String.valueOf(dataBean.getProduct_id()), AllCode.TaoBao);
//            } else {
//                RouterUtils.toGoodsDetail((BaseActivity) context, String.valueOf(dataBean.getProduct_id()), AllCode.Gift);
//            }

//            }else {
//                RouterUtils.toGoodsDetail((BaseActivity) context,String.valueOf(dataBean.getProduct_id()), AllCode.Gift);
//            }
        });
        mSmartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            currentPage = 1;
            getP().getCircle(currentPage);
        });

        mSmartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            currentPage++;
            getP().getCircle(currentPage);
        });
        mSmartRefreshLayout.autoRefresh();

    }

    @OnClick({R.id.iv_back_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back_left:
                finish();
                break;
        }
    }

    public void setData(CicleInfo cicleInfo) {
        List<CicleInfo.DataBean> infos = cicleInfo.getData();
        if (currentPage == 1) {
            if (infos==null||infos.size()==0){
                mStatusManager.showEmpty();
            }else {
                adapter.setNewData(infos);
                mSmartRefreshLayout.finishRefresh(true);
                mStatusManager.showContent();
            }
        } else {
            mSmartRefreshLayout.finishLoadMore(true);
            if (infos == null || infos.size() == 0) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                adapter.addData(infos);
            }
        }
    }

    public void finishRefresh() {
        mSmartRefreshLayout.finishRefresh(true);
        mSmartRefreshLayout.finishLoadMore(true);
    }
}
